package com.hongdee.market.bc.service.impl;

import com.hongdee.common.dao.IHibernateDao;

import com.hongdee.market.bc.dao.ITBcCustomLevelDao;
import com.hongdee.market.bc.entity.TBcCustomeItemScore;
import com.hongdee.market.bc.service.ITBcCustomItemSocreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dell on 2017/5/17.
 * * 客户评级管理
 */
@Service
public class TBcCustomItemSocreImpl implements ITBcCustomItemSocreService {
    @Autowired
    private ITBcCustomLevelDao itBcCustomLevelDao;
    private String tabName = "T_BC_CUSTOME_ITEM_SCORE ";
    @Autowired
    private IHibernateDao iHibernateDao;

    /**
     * 左侧树方法
     */
    @Override
    public List tree(String uid) throws Exception {
        //sql查询
        String sql = "select * from T_BC_CUSTOME_ITEM where status = '1' ";
        return this.itBcCustomLevelDao.list(sql);
    }

    /**
     * 启用停用
     */
    @Override
    public void update(String id, String status) throws Exception {
        String hql = "update TBcCustomeItemScore  set status = :status where id = :id ";
        Map params = new HashMap();
        params.put("id", id);
        params.put("status", status);
        this.itBcCustomLevelDao.update(hql, params);
    }

    /*
      * 根据条件查询
      * */
    @Override
    public Map info(String id) throws Exception {
        String sql = "select * from " + tabName + " where id = ? ";
        return this.itBcCustomLevelDao.info(sql, id);
    }

    @Override
    public List<Map> combo() throws Exception {
        //String sql = " select * from  T_BC_CUSTOME_LEVEL_CONF where status= '1'";
        String sql = " select * from  T_BC_CUSTOME_ITEM where status= '1'";
        List<Map<String, Object>> mapList = this.itBcCustomLevelDao.list(sql);
        List<Map> resList = new ArrayList<Map>();
        Map temp;
        for (Map map : mapList) {
            temp = new HashMap();
            temp.put("label", map.get("itemName"));
            temp.put("value", map.get("id"));
            resList.add(temp);
        }
        return resList;
    }

    /**
     * 新增、修改
     *
     * @param tBcCustomeItemScore
     * @return
     * @throws Exception
     */
    public TBcCustomeItemScore saveOrUpdate(TBcCustomeItemScore tBcCustomeItemScore) throws Exception {
        if (StringUtils.isEmpty(tBcCustomeItemScore.getId()))

        {
            tBcCustomeItemScore.setId(null);
        }
        tBcCustomeItemScore.setStatus("1");
        // 判断是否存在相同评分值
        if (null != tBcCustomeItemScore.getId()) {
            String sql = "select * from T_BC_CUSTOME_ITEM_SCORE where item_id =? and  start = ? and id != ?  and company_id = ? ";
            List ls = this.itBcCustomLevelDao.list(sql, tBcCustomeItemScore.getItemId(), tBcCustomeItemScore.getStart(), tBcCustomeItemScore.getId(), tBcCustomeItemScore.getCompanyId());
            if (ls != null && ls.size() > 0) {
                throw new Exception("该项目下已存在相同的评分值，请修改后保存！");
            }


            sql = "select * from T_BC_CUSTOME_ITEM_SCORE where score = ? and item_id = ? and id != ? and company_id = ? ";
            ls = this.itBcCustomLevelDao.list(sql, tBcCustomeItemScore.getScore(), tBcCustomeItemScore.getItemId(), tBcCustomeItemScore.getId(), tBcCustomeItemScore.getCompanyId());
            if (ls != null && ls.size() > 0) {
                throw new Exception("该项目下已存在相同的分值，请修改后保存！");
            }

        } else

        {
            String sql = "select * from T_BC_CUSTOME_ITEM_SCORE where item_id =? and  start = ? and company_id = ? ";
            List ls = this.itBcCustomLevelDao.list(sql, tBcCustomeItemScore.getItemId(), tBcCustomeItemScore.getStart(), tBcCustomeItemScore.getCompanyId());
            if (ls != null && ls.size() > 0) {
                throw new Exception("该项目下已存在相同的得分，请修改后保存！");
            }


            sql = "select * from T_BC_CUSTOME_ITEM_SCORE where score = ? and item_id = ? and company_id = ? ";
            ls = this.itBcCustomLevelDao.list(sql, tBcCustomeItemScore.getScore(), tBcCustomeItemScore.getItemId(), tBcCustomeItemScore.getCompanyId());
            if (ls != null && ls.size() > 0) {
                throw new Exception("该项目下已存在相同的得分，请修改后保存！");
            }
        }


        this.itBcCustomLevelDao.saveOrUpdate(tBcCustomeItemScore);

        return tBcCustomeItemScore;

    }

    @Override
    public Map newPage(String companyId, String start, String limit, String id) throws Exception {
        String sql = "select * from ( select T_BC_CUSTOME_ITEM_SCORE.*,t_bc_custome_item.item_name from T_BC_CUSTOME_ITEM_SCORE " +
                "LEFT JOIN t_bc_custome_item ON T_BC_CUSTOME_ITEM_SCORE.item_id = t_bc_custome_item.id where T_BC_CUSTOME_ITEM_SCORE.company_id = ? "; // 评分项目ID 评分项目ID

        List<Object> params = new ArrayList();
        params.add(companyId);
        if (!StringUtils.isEmpty(id)) {

            sql += " AND item_id = ? ";
            params.add(id);
        }
        sql += "order by disp_order asc ) as t ";
        return this.itBcCustomLevelDao.page(sql, params.toArray(), start, limit);
    }

    /***
     * 根据公司 ID，项目编码获取设定的评分值
     * @param companyId
     * @param itemId
     * @return
     * @throws Exception
     */
    @Override
    public Map scoreConver(String companyId, String itemId) throws Exception {
        String sql = "select * from T_BC_CUSTOME_ITEM_SCORE where item_id =?  and company_id =? ";
        List params = new ArrayList();
        params.add(itemId);
        params.add(companyId);
        List<Map<String, Object>> ls = this.itBcCustomLevelDao.list(sql, params.toArray());
        Map conv = new HashMap(6);
        for (Map<String, Object> temp : ls) {
            conv.put(temp.get("start"), temp.get("score"));
        }
        return conv;
    }
}